package com.sixiaolong.controller;

import com.baomidou.mybatisplus.core.conditions.interfaces.Func;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sixiaolong.common.BaseContext;
import com.sixiaolong.common.R;
import com.sixiaolong.domain.OrderDetail;
import com.sixiaolong.domain.OrderDto;
import com.sixiaolong.domain.Orders;
import com.sixiaolong.service.OrderDetailService;
import com.sixiaolong.service.OrderService;
import com.sun.org.apache.xpath.internal.operations.Or;
import jdk.nashorn.internal.objects.annotations.Function;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;
//    下单
    @PostMapping("/submit")
    public R<String> saveOrder(@RequestBody Orders orders){
        log.info("orders:"+orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }

//    查询订单 分页
    @GetMapping("/userPage")
    public R<Page> getOrders(int page, int pageSize, String number){
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        Page<OrderDto> pageInfo1 = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Orders::getUserId, BaseContext.getCurrentId());
        lqw.like(Strings.isNotEmpty(number),Orders::getId,number);
        lqw.orderByDesc(Orders::getCheckoutTime);
        orderService.page(pageInfo, lqw);
        BeanUtils.copyProperties(pageInfo,pageInfo1,"records");
        List<OrderDto> orderDtos = pageInfo.getRecords().stream().map(e -> {
            OrderDto orderDto = new OrderDto();
            BeanUtils.copyProperties(e,orderDto);
            LambdaQueryWrapper<OrderDetail> lqw1 = new LambdaQueryWrapper<>();
            lqw1.eq(OrderDetail::getOrderId,e.getId());
            List<OrderDetail> orderDetails = orderDetailService.list(lqw1);
            orderDto.setOrderDetails(orderDetails);
            return orderDto;
        }).collect(Collectors.toList());
        pageInfo1.setRecords(orderDtos);
        return R.success(pageInfo1);
    }

//    全部订单
    @GetMapping("/page")
    public R<Page> getAllOrders(int page, int pageSize, String number, String beginTime, String endTime){
        log.info("begin:"+beginTime+"---endt:"+endTime);
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        Page<OrderDto> pageInfo1 = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(number),Orders::getId,number);
        lqw.ge(StringUtils.isNotBlank(beginTime),Orders::getCheckoutTime,beginTime);
        lqw.lt(StringUtils.isNotBlank(endTime),Orders::getCheckoutTime,endTime);
        lqw.orderByDesc(Orders::getCheckoutTime);
        orderService.page(pageInfo, lqw);
        BeanUtils.copyProperties(pageInfo,pageInfo1,"records");
        List<OrderDto> orderDtos = pageInfo.getRecords().stream().map(e -> {
            OrderDto orderDto = new OrderDto();
            BeanUtils.copyProperties(e,orderDto);
            LambdaQueryWrapper<OrderDetail> lqw1 = new LambdaQueryWrapper<>();
            lqw1.eq(OrderDetail::getOrderId,e.getId());
            List<OrderDetail> orderDetails = orderDetailService.list(lqw1);
            orderDto.setOrderDetails(orderDetails);
            orderDto.setConsignee(e.getConsignee());
            return orderDto;
        }).collect(Collectors.toList());
        pageInfo1.setRecords(orderDtos);
        return R.success(pageInfo1);
    }

//    派送
    @PutMapping
    public R sendOrder(@RequestBody Orders orders){
        orderService.updateById(orders);
        return R.success("派送成功");
    }
}
